package com.teradata.serial.protobuf;

import com.google.protobuf.InvalidProtocolBufferException;
import com.teradata.serial.Serial;
import com.teradata.serial.entities.Products2;

import java.util.ArrayList;
import java.util.List;
import com.teradata.serial.entities.Products2.Products22.Builder;
import com.teradata.serial.entities.Products2.Products22;
public class ProtobufSerial extends Serial {

    public List<byte[]> serializeProtoBufProductsList(
            List<Builder> builderList) {
        if(builderList == null) {
            System.out.println("【ProtoBufSerializeServiceImpl-serializeProtoBufProductsService】builderList==null");
        }
        long start = System.currentTimeMillis();
        List<byte[]> bytesList = new ArrayList<byte[]>();
        for(Products2.Products22.Builder p22Builder : builderList){
            Products2.Products22 p22 = p22Builder.build();
            byte[] bytes = p22.toByteArray();
            bytesList.add(bytes);
        }
        long end = System.currentTimeMillis();
        usedTime = end - start ;
        return bytesList;
    }

    public List<Products22> deserializeProtoBufDataListToProducts22List(
            List<byte[]> bytesList) {
        long start = System.currentTimeMillis();
        List<Products22> list = new ArrayList<Products22>();
        for(byte[] b : bytesList) {
            try {
                list.add(Products2.Products22.parseFrom(b));
            } catch (InvalidProtocolBufferException e) {
                e.printStackTrace();
            }
        }
        long end = System.currentTimeMillis();
        usedTime = end - start;
        return list;
    }
}
